package io.dy.deadlock;

import io.dy.utils.ThreadUtils;

/**
 * @author dmj
 * @date 2024年04月15日 下午2:32
 * 死锁
 */
public class DeadlockMain {

    private final Object sleep_lock = new Object();

    private final Object study_lock = new Object();


    public void study(){
        synchronized (study_lock){
            ThreadUtils.printThreadLog("获取学习锁");
            ThreadUtils.sleepSecond(1);
            synchronized (sleep_lock){
                ThreadUtils.printThreadLog("获取睡觉锁");
            }
        }
    }


    public void sleep(){
        synchronized (sleep_lock){
            ThreadUtils.printThreadLog("获取睡觉锁");
            ThreadUtils.sleepSecond(2);
            synchronized (study_lock){
                ThreadUtils.printThreadLog("获取学习锁");
            }
        }
    }


    public static void main(String[] args) {
        DeadlockMain deadlockMain = new DeadlockMain();

        new Thread(deadlockMain::study,"study").start();

        new Thread(deadlockMain::sleep,"sleep").start();
    }
}
